#include "dragdropplugin.h"
#include "ui_dragdropplugin.h"

#include <QDragEnterEvent>
#include <QMimeData>
#include <ztboxpluginloader.h>

DragDropPlugin::DragDropPlugin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DragDropPlugin)
{
    ui->setupUi(this);
}

DragDropPlugin::~DragDropPlugin()
{
    delete ui;
}

void DragDropPlugin::dragEnterEvent(QDragEnterEvent *event)
{
    if (event->mimeData()->hasUrls()) {
        foreach (const QUrl &url, event->mimeData()->urls()) {
            if (url.fileName().endsWith(".so")) {
                event->acceptProposedAction();
                return;
            }
        }
    }
}

void DragDropPlugin::dropEvent(QDropEvent *event)
{
    QStringList pluginPaths;
    foreach (const QUrl &url, event->mimeData()->urls()) {
        if (url.isLocalFile() && url.fileName().endsWith(".so")) {
            pluginPaths << url.toLocalFile();
        }
    }
    foreach (const QString &path, pluginPaths) {
        ZTBoxPluginLoader::instance().loadPluginFile(path);
    }
    event->acceptProposedAction();
}
